package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Admin;
import com.j48.entrancegurad.model.Host;
import com.j48.entrancegurad.model.Opinion;
import com.j48.entrancegurad.model.User;
import com.j48.entrancegurad.service.IAdminService;
import com.j48.entrancegurad.service.IHostService;
import com.j48.entrancegurad.service.IOpinionService;
import com.j48.entrancegurad.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class OpinionController {
    @Autowired
    private IOpinionService opinionService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IHostService hostService;
    @Autowired
    private IAdminService adminService;

    /**
     * 查询全部未读意见
     * @param opinion 对象
     * @param req
     * @return 跳转的页面
     */
    @RequestMapping("/selectOpinionAll")
    @ResponseBody
    public ModelAndView selectOpinion(Opinion opinion, HttpServletRequest req){
        String username=req.getParameter("userName");
        ModelAndView model=new ModelAndView("jsp/opinion");
        ArrayList<Opinion> opinionlist=new ArrayList<Opinion>();
        ArrayList<Opinion> opinionlist1=new ArrayList<Opinion>();
        ArrayList<User> useropinionlist=new ArrayList<User>();
        ArrayList<Host> hostopinionlist=new ArrayList<Host>();
        ArrayList<Admin> adminopinionlist=new ArrayList<Admin>();
        ArrayList<Integer> useridlist=new ArrayList<Integer>();
        ArrayList<Integer> useridlist1=new ArrayList<Integer>();
        ArrayList<Integer> adminidlist=new ArrayList<Integer>();
        ArrayList<Integer> hostidlist=new ArrayList<Integer>();
        User useropinion =new User();
        Host hostopinion=new Host();
        Admin adminopinion=new Admin();
        User useropinion1 =new User();
        opinion= new Opinion();
        if(username.equals("")){
            opinionlist=opinionService.selectAllNotRead();
            for(int i=0;i<opinionlist.size();i++){
                useridlist.add(i,opinionlist.get(i).getOpinion_user_id());
                adminidlist.add(i,opinionlist.get(i).getOpinion_admin_id());
            }
            for(int i=0;i<useridlist.size();i++){
                adminopinion.setAdmin_id(adminidlist.get(i));
                adminopinionlist.add(i,adminService.selectById(adminopinion));
            }
            for(int i=0;i<adminidlist.size();i++){
                useropinion.setUser_id(useridlist.get(i));
                useropinionlist.add(i,userService.selectByUser_id(useropinion));
            }
            for(int i=0;i<useropinionlist.size();i++){
                hostidlist.add(i,useropinionlist.get(i).getUser_host_id());
            }
            for(int i=0;i<hostidlist.size();i++){
                hostopinion.setHost_id(hostidlist.get(i));
                hostopinionlist.add(i,hostService.selectById(hostopinion));
            }
            req.getSession().setAttribute("opinionlist",opinionlist);
            req.getSession().setAttribute("useropinionlist",useropinionlist);
            req.getSession().setAttribute("hostopinionlist",hostopinionlist);
            req.getSession().setAttribute("adminopinionlist",adminopinionlist);
            return model;
        }else{
            ArrayList<User> userlist=new ArrayList<User>();
            useropinion.setUser_name(username);
            userlist=userService.selectByName(useropinion);
            if(userlist.size()==0){
                req.getSession().removeAttribute("opinionlist");
                req.getSession().removeAttribute("hostopinionlist");
                req.getSession().removeAttribute("adminopinionlist");
                req.getSession().removeAttribute("useropinionlist");
                return model;
            }else{
                for(int i=0;i<userlist.size();i++){
                   // hostidlist.add(userlist.get(i).getUser_host_id());
                    useridlist.add(userlist.get(i).getUser_id());
                }

                for(int i=0;i<useridlist.size();i++){
                    opinion.setOpinion_user_id(useridlist.get(i));
                    opinionlist1=opinionService.selectByUserId(opinion);
                    for(int k=0;k<opinionlist1.size();k++){
                    opinionlist.add(opinionlist1.get(k));
                    }
                }
                User user5=new User();
                if (opinionlist.size() > 0) {
                   for(int i=0;i<opinionlist.size();i++){
                       user5.setUser_id(opinionlist.get(i).getOpinion_user_id());
                       hostidlist.add(userService.selectByUser_id(user5).getUser_host_id());
                   }
                    for(int i=0;i<hostidlist.size();i++){
                        hostopinion.setHost_id(hostidlist.get(i));
                        hostopinionlist.add(i,hostService.selectById(hostopinion));
                    }
                }else{
                    req.getSession().removeAttribute("opinionlist");
                    req.getSession().removeAttribute("hostopinionlist");
                    req.getSession().removeAttribute("adminopinionlist");
                    req.getSession().removeAttribute("useropinionlist");
                    return model;
                }
                }
                for(int i=0;i<opinionlist.size();i++){
                    useridlist1.add(i, opinionlist.get(i).getOpinion_user_id());
                }
                for(int i=0;i<useridlist1.size();i++){
                    useropinion1.setUser_id(useridlist1.get(i));
                    useropinionlist.add(i,userService.selectByUser_id(useropinion1));
                }
                for(int i=0;i<opinionlist.size();i++){
                    adminopinion.setAdmin_id(opinionlist.get(i).getOpinion_admin_id());
                    adminopinionlist.add(i,adminService.selectById(adminopinion));
                }
                req.getSession().setAttribute("opinionlist",opinionlist);
                req.getSession().setAttribute("useropinionlist",useropinionlist);
                req.getSession().setAttribute("hostopinionlist",hostopinionlist);
                req.getSession().setAttribute("adminopinionlist",adminopinionlist);
            }
        return model;
    }

    /**
     * 确认已读意见
     * @param req
     * @param opinion
     * @return
     */
    @RequestMapping("/alreadyread")
    @ResponseBody
    public String hasread(HttpServletRequest req,Opinion opinion){
        String opinionids[]=req.getParameterValues("opid");
        Admin admin= (Admin) req.getSession().getAttribute("admin");
        opinion=new Opinion();
        ArrayList<Integer> opinionidall=new ArrayList<Integer>();
        if(opinionids==null){
            return "false";
        }
        for(int i=0;i<opinionids.length;i++){
            opinionidall.add(Integer.parseInt(opinionids[i]));
        }
        Timestamp time=new Timestamp(new Date().getTime());
        for(int i=0;i<opinionidall.size();i++){
            opinion.setOpinion_id(opinionidall.get(i));
            opinion.setOpinion_lasttime(time);
            opinion.setOpinion_admin_id(admin.getAdmin_id());
            opinionService.updatestatus(opinion);
        }
        return "true";
    }

    /**
     * 查询全部未读意见
     * @param req
     * @return
     */
    @RequestMapping("/selectopinionalls")
    @ResponseBody
    public String selectall(HttpServletRequest req){
        ArrayList<Opinion> opinionlist=new ArrayList<Opinion>();
        ArrayList<User> useropinionlist=new ArrayList<User>();
        ArrayList<Host> hostopinionlist=new ArrayList<Host>();
        ArrayList<Admin> adminopinionlist=new ArrayList<Admin>();
        ArrayList<Integer> useridlist=new ArrayList<Integer>();
        ArrayList<Integer> adminidlist=new ArrayList<Integer>();
        ArrayList<Integer> hostidlist=new ArrayList<Integer>();
        User useropinion =new User();
        Host hostopinion=new Host();
        Admin adminopinion=new Admin();
        ArrayList<Opinion> opinionlistAll=new ArrayList<Opinion>();
        ArrayList<User> useropinionlist1=new ArrayList<User>();
        ArrayList<Host> hostopinionlist1=new ArrayList<Host>();
        ArrayList<Admin> adminopinionlist1=new ArrayList<Admin>();
        ArrayList<Integer> useridlist1=new ArrayList<Integer>();
        ArrayList<Integer> adminidlist1=new ArrayList<Integer>();
        ArrayList<Integer> hostidlist1=new ArrayList<Integer>();
        User useropinion1 =new User();
        Host hostopinion1=new Host();
        Admin adminopinion1=new Admin();
        opinionlist=opinionService.selectAllNotRead();
        opinionlistAll=opinionService.selectAll();
        for(int i=0;i<opinionlist.size();i++){
            useridlist.add(i,opinionlist.get(i).getOpinion_user_id());
            adminidlist.add(i,opinionlist.get(i).getOpinion_admin_id());
        }
        for(int i=0;i<useridlist.size();i++){
            adminopinion.setAdmin_id(adminidlist.get(i));
            adminopinionlist.add(i,adminService.selectById(adminopinion));
        }
        for(int i=0;i<adminidlist.size();i++){
            useropinion.setUser_id(useridlist.get(i));
            useropinionlist.add(i,userService.selectByUser_id(useropinion));
        }
        for(int i=0;i<useropinionlist.size();i++){
            hostidlist.add(i,useropinionlist.get(i).getUser_host_id());
        }
        for(int i=0;i<hostidlist.size();i++){
            hostopinion.setHost_id(hostidlist.get(i));
            hostopinionlist.add(i,hostService.selectById(hostopinion));
        }
        for(int i=0;i<opinionlistAll.size();i++){
            useridlist1.add(i,opinionlistAll.get(i).getOpinion_user_id());
            adminidlist1.add(i,opinionlistAll.get(i).getOpinion_admin_id());
        }
        for(int i=0;i<useridlist1.size();i++){
            adminopinion1.setAdmin_id(adminidlist1.get(i));
            adminopinionlist1.add(i,adminService.selectById(adminopinion1));
        }
        for(int i=0;i<adminidlist1.size();i++){
            useropinion1.setUser_id(useridlist1.get(i));
            useropinionlist1.add(i,userService.selectByUser_id(useropinion1));
        }
        for(int i=0;i<useropinionlist1.size();i++){
            hostidlist1.add(i,useropinionlist1.get(i).getUser_host_id());
        }
        for(int i=0;i<hostidlist1.size();i++){
            hostopinion1.setHost_id(hostidlist1.get(i));
            hostopinionlist1.add(i,hostService.selectById(hostopinion1));
        }
        req.getSession().setAttribute("opinionlist",opinionlist);
        req.getSession().setAttribute("useropinionlist",useropinionlist);
        req.getSession().setAttribute("hostopinionlist",hostopinionlist);
        req.getSession().setAttribute("adminopinionlist",adminopinionlist);
        req.getSession().setAttribute("opinionlistAll",opinionlistAll);
        req.getSession().setAttribute("useropinionlist1",useropinionlist1);
        req.getSession().setAttribute("hostopinionlist1",hostopinionlist1);
        req.getSession().setAttribute("adminopinionlist1",adminopinionlist1);
        return "true";
    }

    /**
     * 查询全部未读意见
     * @param opinion 对象
     * @param req
     * @return 跳转的页面
     */
    @RequestMapping("/selectOpinionAll1")
    @ResponseBody
    public ModelAndView selectOpinion1(Opinion opinion, HttpServletRequest req){
        String username=req.getParameter("userName");
        ModelAndView model=new ModelAndView("jsp/opinionAll");
        ArrayList<Opinion> opinionlist=new ArrayList<Opinion>();
        ArrayList<Opinion> opinionlist1=new ArrayList<Opinion>();
        ArrayList<Opinion> opinionlist2=new ArrayList<Opinion>();
        ArrayList<User> useropinionlist=new ArrayList<User>();
        ArrayList<Host> hostopinionlist=new ArrayList<Host>();
        ArrayList<Admin> adminopinionlist=new ArrayList<Admin>();
        ArrayList<Integer> useridlist=new ArrayList<Integer>();
        ArrayList<Integer> useridlist1=new ArrayList<Integer>();
        ArrayList<Integer> adminidlist=new ArrayList<Integer>();
        ArrayList<Integer> hostidlist=new ArrayList<Integer>();
        ArrayList<Integer> hostidlist1=new ArrayList<Integer>();
        User useropinion =new User();
        Host hostopinion=new Host();
        Host hostopinion1=new Host();
        Host host=new Host();
        Admin adminopinion=new Admin();
        User useropinion1 =new User();
        opinion= new Opinion();
        if(username.equals("")){
            opinionlist=opinionService.selectAll();
            for(int i=0;i<opinionlist.size();i++){
                useridlist.add(i,opinionlist.get(i).getOpinion_user_id());
                adminidlist.add(i,opinionlist.get(i).getOpinion_admin_id());
            }
            for(int i=0;i<useridlist.size();i++){
                adminopinion.setAdmin_id(adminidlist.get(i));
                adminopinionlist.add(i,adminService.selectById(adminopinion));
            }
            for(int i=0;i<adminidlist.size();i++){
                useropinion.setUser_id(useridlist.get(i));
                useropinionlist.add(i,userService.selectByUser_id(useropinion));
            }
            for(int i=0;i<useropinionlist.size();i++){
                hostidlist.add(i,useropinionlist.get(i).getUser_host_id());
            }
            for(int i=0;i<hostidlist.size();i++){
                hostopinion.setHost_id(hostidlist.get(i));
                hostopinionlist.add(i,hostService.selectById(hostopinion));
            }
            req.getSession().setAttribute("opinionlistAll",opinionlist);
            req.getSession().setAttribute("useropinionlist1",useropinionlist);
            req.getSession().setAttribute("hostopinionlist1",hostopinionlist);
            req.getSession().setAttribute("adminopinionlist1",adminopinionlist);
            return model;
        }else{
            ArrayList<User> userlist=new ArrayList<User>();
            useropinion.setUser_name(username);
            userlist=userService.selectByName(useropinion);
            if(userlist.size()==0){
                req.getSession().removeAttribute("opinionlistAll");
                req.getSession().removeAttribute("hostopinionlist1");
                req.getSession().removeAttribute("adminopinionlist1");
                req.getSession().removeAttribute("useropinionlist1");
                return model;
            }else{
                for(int i=0;i<userlist.size();i++){
                    hostidlist.add(userlist.get(i).getUser_host_id());
                    useridlist.add(userlist.get(i).getUser_id());
                }
//                for(int i=0;i<hostidlist.size();i++){
//                    hostopinion.setHost_id(hostidlist.get(i));
//                    hostopinionlist.add(i,hostService.selectById(hostopinion));
//                }
                for(int i=0;i<useridlist.size();i++) {
                    opinion.setOpinion_user_id(useridlist.get(i));
                    opinionlist1 = opinionService.selectByUserId1(opinion);
                    for (int s = 0; s < opinionlist1.size(); s++) {
                        opinionlist2.add(opinionlist1.get(s));
                    }
                }
                    if(opinionlist2.size()!=0){
                    for(int k=0;k<opinionlist2.size();k++){
                        opinionlist.add(opinionlist2.get(k));
                    }
                    }else{
                        req.getSession().removeAttribute("opinionlistAll");
                        req.getSession().removeAttribute("hostopinionlist1");
                        req.getSession().removeAttribute("adminopinionlist1");
                        req.getSession().removeAttribute("useropinionlist1");
                        return model;
                    }
                for(int i=0;i<opinionlist.size();i++){
                    useridlist1.add(i, opinionlist.get(i).getOpinion_user_id());
                }
                for(int i=0;i<useridlist1.size();i++){
                    useropinion1.setUser_id(useridlist1.get(i));
                    useropinionlist.add(i,userService.selectByUser_id(useropinion1));
                }
                for(int i=0;i<useropinionlist.size();i++){
                    hostopinion1.setHost_id(useropinionlist.get(i).getUser_host_id());
                   host= hostService.selectById(hostopinion1);
                    hostopinionlist.add(i,host);
                }
                for(int i=0;i<opinionlist.size();i++){
                    adminopinion.setAdmin_id(opinionlist.get(i).getOpinion_admin_id());
                    adminopinionlist.add(i,adminService.selectById(adminopinion));
                }
                req.getSession().setAttribute("opinionlistAll",opinionlist);
                req.getSession().setAttribute("useropinionlist1",useropinionlist);
                req.getSession().setAttribute("hostopinionlist1",hostopinionlist);
                req.getSession().setAttribute("adminopinionlist1",adminopinionlist);
            }
        }
        return model;
    }

    /**
     * 查询全部意见
     * @param request
     * @return
     */
    @RequestMapping("showAllOpinion")
    @ResponseBody
    public String all(HttpServletRequest request){
        ArrayList<Opinion> opinionlistAll=new ArrayList<Opinion>();
        ArrayList<User> useropinionlist1=new ArrayList<User>();
        ArrayList<Host> hostopinionlist1=new ArrayList<Host>();
        ArrayList<Admin> adminopinionlist1=new ArrayList<Admin>();
        ArrayList<Integer> useridlist1=new ArrayList<Integer>();
        ArrayList<Integer> adminidlist1=new ArrayList<Integer>();
        ArrayList<Integer> hostidlist1=new ArrayList<Integer>();
        User useropinion =new User();
        Host hostopinion=new Host();
        Admin adminopinion=new Admin();
        User useropinion1 =new User();
        Host hostopinion1=new Host();
        Admin adminopinion1=new Admin();
        opinionlistAll=opinionService.selectAll();
        for(int i=0;i<opinionlistAll.size();i++){
            useridlist1.add(i,opinionlistAll.get(i).getOpinion_user_id());
            adminidlist1.add(i,opinionlistAll.get(i).getOpinion_admin_id());
        }
        for(int i=0;i<useridlist1.size();i++){
            adminopinion1.setAdmin_id(adminidlist1.get(i));
            adminopinionlist1.add(i,adminService.selectById(adminopinion1));
        }
        for(int i=0;i<adminidlist1.size();i++){
            useropinion1.setUser_id(useridlist1.get(i));
            useropinionlist1.add(i,userService.selectByUser_id(useropinion1));
        }
        for(int i=0;i<useropinionlist1.size();i++){
            hostidlist1.add(i,useropinionlist1.get(i).getUser_host_id());
        }
        for(int i=0;i<hostidlist1.size();i++){
            hostopinion1.setHost_id(hostidlist1.get(i));
            hostopinionlist1.add(i,hostService.selectById(hostopinion1));
        }
        request.getSession().setAttribute("opinionlistAll",opinionlistAll);
        request.getSession().setAttribute("useropinionlist1",useropinionlist1);
        request.getSession().setAttribute("hostopinionlist1",hostopinionlist1);
        request.getSession().setAttribute("adminopinionlist1",adminopinionlist1);
        return  "true";
    }

    /**
     * 查询全部已处理意见
     * @param request
     * @return
     */
    @RequestMapping("showAllfinishedOpinion")
    @ResponseBody
    public String finishedOpinion(HttpServletRequest request){
        ArrayList<Opinion> opinionlistAll=new ArrayList<Opinion>();
        ArrayList<User> useropinionlist1=new ArrayList<User>();
        ArrayList<Host> hostopinionlist1=new ArrayList<Host>();
        ArrayList<Admin> adminopinionlist1=new ArrayList<Admin>();
        ArrayList<Integer> useridlist1=new ArrayList<Integer>();
        ArrayList<Integer> adminidlist1=new ArrayList<Integer>();
        ArrayList<Integer> hostidlist1=new ArrayList<Integer>();
        User useropinion =new User();
        Host hostopinion=new Host();
        Admin adminopinion=new Admin();
        User useropinion1 =new User();
        Host hostopinion1=new Host();
        Admin adminopinion1=new Admin();
        opinionlistAll=opinionService.selectAllfinished();
        for(int i=0;i<opinionlistAll.size();i++){
            useridlist1.add(i,opinionlistAll.get(i).getOpinion_user_id());
            adminidlist1.add(i,opinionlistAll.get(i).getOpinion_admin_id());
        }
        for(int i=0;i<useridlist1.size();i++){
            adminopinion1.setAdmin_id(adminidlist1.get(i));
            adminopinionlist1.add(i,adminService.selectById(adminopinion1));
        }
        for(int i=0;i<adminidlist1.size();i++){
            useropinion1.setUser_id(useridlist1.get(i));
            useropinionlist1.add(i,userService.selectByUser_id(useropinion1));
        }
        for(int i=0;i<useropinionlist1.size();i++){
            hostidlist1.add(i,useropinionlist1.get(i).getUser_host_id());
        }
        for(int i=0;i<hostidlist1.size();i++){
            hostopinion1.setHost_id(hostidlist1.get(i));
            hostopinionlist1.add(i,hostService.selectById(hostopinion1));
        }
        request.getSession().setAttribute("opinionlistAll",opinionlistAll);
        request.getSession().setAttribute("useropinionlist1",useropinionlist1);
        request.getSession().setAttribute("hostopinionlist1",hostopinionlist1);
        request.getSession().setAttribute("adminopinionlist1",adminopinionlist1);
        return  "true";
    }

    /**
     * 删除意见
     * @param req
     * @param opinion
     * @return
     */
    @RequestMapping("/alreadydeleteopinion")
    @ResponseBody
    public String deleteopinion(HttpServletRequest req,Opinion opinion){
        int opiniondeleteid=Integer.parseInt(req.getParameter("hiddenvalue"));
        opinion.setOpinion_id(opiniondeleteid);
        Opinion opinion1=new Opinion();
        opinion1=opinionService.selectById(opinion);
        int status=opinion1.getOpinion_status();
        if(status==0){
            return "false";
        }if(status==1){
        opinionService.deleteById(opinion);
            return "true";
        }else{
            return "true";
        }
    }
}
